package com.example.myapplication

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import com.example.myapplication.MyDbHelper
object MyQueryHelper {//用于在任何一个地方来调用表格数据
    private lateinit var dbAdapter: MyDbAdapter
    var list = mutableListOf<String>()
    @SuppressLint("Range")
    fun queryDataById(context: Context, idNum: String): List<String> {
        val selection = "${MyDbHelper.COLUMN_ID_NUM} = ?"
        val selectionArgs = arrayOf(idNum.toString())
        dbAdapter = MyDbAdapter(context)
        val cursor = dbAdapter.query(
            null,
            selection,
            selectionArgs,
            null,
            null,
            null
        )
        if (cursor.moveToFirst()) {
            val idNum = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_ID_NUM))
            val name = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_NAME))
            val graph1 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_GRAPH1))
            val graph2 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_GRAPH2))
            val graph3 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_GRAPH3))
            val graph4 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_GRAPH4))
            val model1 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_MODEL1))
            val model2 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_MODEL2))
            val model3 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_MODEL3))
            val text1 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_TEXT1))
            val text2 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_TEXT2))
            val text3 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_TEXT3))
            val text4 = cursor.getString(cursor.getColumnIndex(MyDbHelper.COLUMN_TEXT4))
            // 在此处添加显示数据的代码，例如使用Log打印
            Log.d("MainActivity", "idNum: $idNum, name: $name, graph1: $graph1, graph2: $graph2, graph3: $graph3, graph4: $graph4, model1: $model1, model2: $model2, model3: $model3, text1: $text1, text2: $text2, text3: $text3, text4: $text4")
            list.clear()
            list.add(idNum)
            list.add(name)
            list.add(graph1)
            list.add(graph2)
            list.add(graph3)
            list.add(graph4)
            list.add(model1)
            list.add(model2)
            list.add(model3)
            list.add(text1)
            list.add(text2)
            list.add(text3)
            list.add(text4)
        } else {
            // 如果查询结果为空，则在此处添加相应的处理代码，例如给出提示信息
            Log.d("MainActivity", "查询结果为空")
        }
        cursor.close()
        return list
    }
}